package com.androiddoc.randomgame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androiddoc.randomgame.mycompnents.FlashView;
import com.androiddoc.randomgame.util.CommHelper;
import com.flurry.android.FlurryAgent;
import com.androiddoc.randomgame.R;

public class GamePoker extends Activity implements View.OnClickListener{
	
	private final static int KEY_TONGHUASHUN = 0;
	private final static int KEY_SHUNZI = 1;
	private final static int KEY_TONGHUA = 2;
	private final static int KEY_HULU = 3;
	private final static int KEY_LIANGDUI = 4;
	private final static int KEY_SANTIAO = 5;
	private final static int KEY_YIDUI = 6;
	
	enum POKER_COLOR{
		Spade, Heart, Diamond, Club
	}
	class Poker{
		int num;
		POKER_COLOR color;
		int resouceIndex;
	}
	private final static int[] POKER_CARDS = {
		R.drawable.black_heart_2,
		R.drawable.black_heart_3, 
		R.drawable.black_heart_4,
		R.drawable.black_heart_5, 
		R.drawable.black_heart_6,
		R.drawable.black_heart_7, 
		R.drawable.black_heart_8,
		R.drawable.black_heart_9, 
		R.drawable.black_heart_10,
		R.drawable.poker_black_heart_j,
		R.drawable.poker_black_heart_q, 
		R.drawable.poker_black_heart_k,
		R.drawable.poker_black_heart_a,
		R.drawable.red_heart_2,
		R.drawable.red_heart_3,
		R.drawable.red_heart_4,
		R.drawable.red_heart_5,
		R.drawable.red_heart_6,
		R.drawable.red_heart_7,
		R.drawable.red_heart_8,
		R.drawable.red_heart_9,
		R.drawable.red_heart_10,
		R.drawable.poker_red_heart_j,
		R.drawable.poker_red_heart_q,
		R.drawable.poker_red_heart_k,
		R.drawable.poker_red_heart_a,
		R.drawable.diamond_2,
		R.drawable.diamond_3,
		R.drawable.diamond_4,
		R.drawable.diamond_5,
		R.drawable.diamond_6,
		R.drawable.diamond_7,
		R.drawable.diamond_8,
		R.drawable.diamond_9,
		R.drawable.diamond_10,
		R.drawable.poker_diamond_j,
		R.drawable.poker_diamond_q,
		R.drawable.poker_diamond_k,
		R.drawable.poker_diamond_a,
		R.drawable.club_2,
		R.drawable.club_3,
		R.drawable.club_4,
		R.drawable.club_5,
		R.drawable.club_6,
		R.drawable.club_7,
		R.drawable.club_8,
		R.drawable.club_9,
		R.drawable.club_10,
		R.drawable.poker_club_j,
		R.drawable.poker_club_q,
		R.drawable.poker_club_k,
		R.drawable.poker_club_a,
		};
	private int currentIndex = 0;
	private LinearLayout result_list;
	private TextView resultText;
	private FlashView poker;
	private boolean isRolling = false;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.poker);
        result_list = (LinearLayout) findViewById(R.id.result_list);
        poker = (FlashView) findViewById(R.id.coin);
        resultText = (TextView) findViewById(R.id.result_text);
        createFlashPoker();
    }
    
    private void createFlashPoker(){
    	poker.initFlashBitmaps(new int[]{
        		R.drawable.poker_track
        		});
        poker.start();
    }
    
    private int[] randomIds = {-1,-1,-1,-1,-1};
    private int getDistinctId(){
    	int idx = (int) (Math.random() * 52);
    	boolean hasRepeat = false;
    	for(int i = 0;i < randomIds.length; i++){
			if(randomIds[i] == idx){
				Log.d("TAG", "hasRepeat:" + idx);
				hasRepeat = true;
				break;
			}
		}
    	if(hasRepeat){
    		idx = getDistinctId();
    	}
    	return idx;
    }
    
    @Override
    public void onClick(View v) {
    	FlurryAgent.logEvent("poker_game");
    	switch(v.getId()){
    	case R.id.play:
    		if(isRolling) {
    			return;
    		}
    		resultText.setText("");
    		isRolling = true;
    		result_list.removeAllViews();
    		pokerAnimation();
    		break;
    		default:
    			break;
    	}

    	
    }
    
    private void pokerAnimation(){
    	if(currentIndex > 4){
    		isRolling = false;
    		currentIndex = 0;
    		int[] newArray = analysis(randomIds);
    		result_list.removeAllViews();
    		for(int i = 0; i < newArray.length; i++){
    			addViewToList(newArray[i]);
    		}
    		return;
    	}
		int idx = getDistinctId();
		randomIds[currentIndex] = idx;
		Log.d("TAG", "idx:" + idx);
		ImageView pokerView = addViewToList(idx);
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.fly);
		hyperspaceJumpAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				currentIndex ++;
				pokerAnimation();
			}
		});
		pokerView.startAnimation(hyperspaceJumpAnimation);
    }
    
    private ImageView addViewToList(int index){
    	ImageView pokerView = new ImageView(this);
		pokerView.setLayoutParams(new LayoutParams(
				CommHelper.dip2px(GamePoker.this, 60)
				, 
				CommHelper.dip2px(GamePoker.this, 90)));
		pokerView.setBackgroundResource(POKER_CARDS[index]);
		result_list.addView(pokerView);
    	return pokerView;
    }
    
    /**
     * acording the result to analysis a judgement
     * @param gameResult
     * @return
     * @return
     * @author xiexiaojian
     * @date 2013-1-6
     */
    @SuppressLint({ "ParserError", "ParserError" })
	private int[] analysis(int[] gameResult){
//    	gameResult = new int[]{14,15,16,17,18};
    	List<Poker> pokers = new ArrayList<Poker>();
    	Poker pokerTemp;
    	int resourceIndex;
    	for(int i = 0; i < gameResult.length; i++){
    		pokerTemp = new Poker();
    		resourceIndex = gameResult[i];
    		if(resourceIndex >= 0 && resourceIndex <= 12){
    			pokerTemp.color = POKER_COLOR.Spade;
    		} else if(resourceIndex >= 13 && resourceIndex <= 25){
    			pokerTemp.color = POKER_COLOR.Heart;
    		} else if(resourceIndex >= 26 && resourceIndex <= 38){
    			pokerTemp.color = POKER_COLOR.Diamond;
    		} else if(resourceIndex >= 39 && resourceIndex <= 51){
    			pokerTemp.color = POKER_COLOR.Club;
    		}
    		pokerTemp.num = resourceIndex % 13 + 2;
    		pokerTemp.resouceIndex = resourceIndex;
    		pokers.add(pokerTemp);
    	}
    	sort(pokers);
    	boolean checkConnect = checkConnect(pokers);
    	boolean checkSameColor = checkSameColor(pokers);
    	boolean checkHulu = checkTwoAndThree(pokers);
    	boolean checkThree = checkThree(pokers);
    	boolean checkDoubleTwo = checkDoubleTwo(pokers);
    	boolean checkTwo = checkTwo(pokers);
    	int result = 0;
    	if(checkConnect && checkSameColor){
    		result = KEY_TONGHUASHUN;
    		resultText.setText("同花顺");
    	} else if(checkConnect){
    		result = KEY_SHUNZI;
    		resultText.setText("顺子");
    	} else if(checkSameColor){
    		result = KEY_TONGHUA;
    		resultText.setText("同花");
    	} else if(checkHulu){
    		result = KEY_HULU;
    		resultText.setText("葫芦");
    	} else if(checkDoubleTwo){
    		result = KEY_LIANGDUI;
    		resultText.setText("两对");
    	} else if(checkThree){
    		result = KEY_SANTIAO;
    		resultText.setText("三条");
    	} else if(checkTwo){
    		result = KEY_YIDUI;
    		resultText.setText("一对");
    	} else {
    		resultText.setText("普通牌");
    	}
    	int[] resultArray = new int[pokers.size()];
    	for(int i = 0; i < resultArray.length; i++){
    		resultArray[i] = pokers.get(i).resouceIndex;
    	}
    	return resultArray;
    }
    
    /**
     * 濮��锟斤拷锟斤拷
     * @param pokers
     * @return
     * @author xiexiaojian
     * @date 2013-1-6
     */
    private void sort(List<Poker> pokers){
    	Comparator<Poker> comparator = new Comparator<Poker>() {
			
			@Override
			public int compare(Poker lhs, Poker rhs) {
				return lhs.num - rhs.num;
			}
		};
		Collections.sort(pokers, comparator);
    }
    
    private boolean checkConnect(List<Poker> pokers){
    	boolean connect = true;
    	for(int i = 1; i < pokers.size(); i++){
    		if(connect){
    			connect = pokers.get(i).num - 1 == pokers.get(i - 1).num;
    		} else {
    			connect = false;
    		}
    	}
    	return connect;
    }
    
    private boolean checkSameColor(List<Poker> pokers){
    	boolean sameColor = true;
    	for(int i = 1; i < pokers.size(); i++){
    		if(sameColor){
    			sameColor = pokers.get(i).color.name().equals(pokers.get(i - 1).color.name());
    		} else {
    			sameColor = false;
    		}
    	}
    	return sameColor;
    }
    
    private boolean checkTwoAndThree(List<Poker> pokers){
    	boolean hulu = false;
    	int poker1 = pokers.get(0).num;
    	int poker2 = pokers.get(1).num;
    	int poker3 = pokers.get(2).num;
    	int poker4 = pokers.get(3).num;
    	int poker5 = pokers.get(4).num;
    	if(((poker1 == poker2 && poker2 == poker3) && (poker4 == poker5))
    			|| ((poker1 == poker2) && (poker3 == poker4 && poker4 == poker5))){
    		hulu = true;
    	}
    	return hulu;
    }
    
    private boolean checkThree(List<Poker> pokers){
    	boolean three = false;
    	int poker1 = pokers.get(0).num;
    	int poker2 = pokers.get(1).num;
    	int poker3 = pokers.get(2).num;
    	int poker4 = pokers.get(3).num;
    	int poker5 = pokers.get(4).num;
    	if((poker1 == poker2 && poker2 == poker3)
    			|| (poker2 == poker3 && poker3 == poker4)
    			|| (poker3 == poker4 && poker4 == poker5)){
    		three = true;
    	}
    	return three;
    }
    
    private boolean checkTwo(List<Poker> pokers){
    	boolean two = false;
    	for(int i = 1; i < pokers.size(); i++){
    		two = pokers.get(i).num == pokers.get(i - 1).num;
    		if(two){
    			break;
    		}
    	}
    	return two;
    }
    
    private boolean checkDoubleTwo(List<Poker> pokers){
    	boolean doubleTwo = false;
    	int poker1 = pokers.get(0).num;
    	int poker2 = pokers.get(1).num;
    	int poker3 = pokers.get(2).num;
    	int poker4 = pokers.get(3).num;
    	int poker5 = pokers.get(4).num;
    	if((poker1 == poker2 && poker3 == poker4)
    			|| (poker1 == poker2 && poker4 == poker5)
    			|| (poker2 == poker3 && poker4 == poker5)){
    		doubleTwo = true;
    	}
    	return doubleTwo;
    }
}
